package com.lucifer.cloud.boot.blog.service.impl;

import com.alibaba.nacos.shaded.com.google.common.collect.Maps;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lucifer.cloud.boot.blog.domin.dto.upload.UploadReq;
import com.lucifer.cloud.boot.blog.domin.type.BlogConstant;
import com.lucifer.cloud.boot.blog.config.OSSUtil;
import com.lucifer.cloud.boot.blog.domin.bo.Upload;
import com.lucifer.cloud.boot.blog.domin.dto.upload.UploadConverter;
import com.lucifer.cloud.boot.blog.mapper.UploadMapper;
import com.lucifer.cloud.boot.blog.service.UploadService;
import com.lucifer.cloud.boot.blog.config.UserSystem;
import com.lucifer.cloud.commons.model.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.HttpStatus;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;
import java.util.Objects;
/**
 * @author lucifer
 * @date 2023/12/21 15:24
 */
@Service
public class UploadServiceImpl extends ServiceImpl<UploadMapper, Upload> implements UploadService {

    @Resource
    OSSUtil ossUtil;

    @Resource
    private UserSystem userSystem;

    @Resource
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Override
    public Result upload(HttpServletRequest request,String path,String file_name,MultipartFile file) {
        Long userId = userSystem.userId(request);
        if(Objects.isNull(userId)){
            return Result.error(HttpStatus.UNAUTHORIZED.value(),BlogConstant.NOT_LOGIN);
        }
        // ossUtil.putImgObject 上传图片非常耗时，此处用异步处理
        threadPoolTaskExecutor.execute(() -> {
           ossUtil.putImgObject(path,file_name,file);
        });
        Upload upload = UploadConverter.convertReq2Upload(userId, file_name, path, file,null);
        save(upload);
        Map<String, String> imgInfo = ossUtil.getImgInfo(path, file_name);
        return Result.success(UploadConverter.convertImgRes2Res(imgInfo,upload));
    }

    @Override
    public String simpleUpload(String path, String file_name, MultipartFile file) {
        ossUtil.putImgObject(path,file_name,file);
//        threadPoolTaskExecutor.execute(() -> {
//            ossUtil.putImgObject(path,file_name,file);
//        });
        https://oss.lucifer-cloud.com/blog/chat/image/%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE%202025-03-12%20160827.jpg
        return "https://oss.lucifer-cloud.com/blog/" + path  + "/"  + file.getOriginalFilename();
    }

    @Override
    public Result save(HttpServletRequest request, UploadReq req) {
        Long userId = userSystem.userId(request);
        if(Objects.isNull(userId)){
            return Result.error(HttpStatus.UNAUTHORIZED.value(),BlogConstant.NOT_LOGIN);
        }
        Upload upload = UploadConverter.convertReq2Upload(userId, req.getFile_name(), req.getPath(), null, req.getFile_size());
        save(upload);
        Map map = Maps.newHashMap();
        map.put("file_id",String.valueOf(upload.getId()));
        return Result.success(map);
    }

    @Override
    public Result delete(HttpServletRequest request, String key) {
        Long userId = userSystem.userId(request);
        if(Objects.isNull(userId)){
            return Result.error(HttpStatus.UNAUTHORIZED.value(),BlogConstant.NOT_LOGIN);
        }
        String fileName = UploadConverter.convertKey2FileName(key);
        Boolean deleteObject = ossUtil.deleteObject(key);
        if (deleteObject){
            remove(Wrappers.lambdaQuery(Upload.class)
                    .eq(Objects.nonNull(userId),Upload::getUser_id,userId)
                    .eq(Upload::getFile_name,fileName)
            );
        }
        return Result.success(deleteObject);
    }
}
